import axios, { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse,AxiosError } from 'axios'
import { showToast } from 'vant';

const baseUrl: string = import.meta.env.VITE_BASE_URL

const service: AxiosInstance = axios.create({
    baseURL: baseUrl,
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
})

service.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        return config
    },
    (error: AxiosError) => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (res: AxiosResponse) => {
        if (res.data.code !== 0) {
            showToast(res.data.msg)
            return Promise.reject(res.data.msg);
        }
        return res
    },
    (error: AxiosError) => {
        return Promise.reject(error);
    }
)

export default service